package kr.pe.dothome.yyar.myphone.controller;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;

import android.util.Log;

import kr.pe.dothome.yyar.myphone.domain.MissedMessage;
import kr.pe.dothome.yyar.myphone.domain.Request;

public class Responser {
	private long startDate;
	private long endDate;
	
	private ContentResolver cr;
	
	public Responser(ContentResolver cr, long startDate) {
		this(cr, startDate, System.currentTimeMillis() / 1000);
	}
	
	public Responser(ContentResolver cr, long startDate, long endDate) {
		this.cr = cr;
		this.startDate = startDate;
		this.endDate = endDate;
	}
	
	public Request getRequestResponse(String rid) {
		Request req = new Request(rid);
		
		addMissedCallLog(req);
		addMissedSms(req);
		addMissedMms(req);
		
		return req;
	}

	private void addMissedCallLog(Request req) {
		Uri allMessages = CallLog.Calls.CONTENT_URI;
		final String[] projection = {"_id", "number", "name", "date"};
		final String queryOption = CallLog.Calls.TYPE + "= ? and date between ? and ?";
		final String[] optionValue = {String.valueOf(CallLog.Calls.MISSED_TYPE), String.valueOf(startDate * 1000), String.valueOf(endDate * 1000)};

		Cursor cursor = cr.query(allMessages, projection,
				queryOption, optionValue, CallLog.Calls.DEFAULT_SORT_ORDER);
		
		while (cursor.moveToNext()) {
			MissedMessage msg = MissedMessage.callLog(req.getRequestId(), 
												cursor.getString(cursor.getColumnIndex("name")),
												cursor.getString(cursor.getColumnIndex("number")),
												cursor.getLong(cursor.getColumnIndex("date")) / 1000);
			req.addMesssage(msg);
		}

	}
	
	private void addMissedSms(Request req) {
		Uri allMessages = Uri.parse("content://sms/inbox");
		final String[] projection = { "_id", "read", "type", "phonebook_name", "address", "date", "body"};
		final String queryOption = "read = ? and type = ? and date between ? and ?";
		final String[] optionValue = {"0", "1", String.valueOf(startDate * 1000), String.valueOf(endDate * 1000)};

		Cursor cursor = cr.query(allMessages, projection,
				queryOption, optionValue, null);
		
		while (cursor.moveToNext()) {			
			MissedMessage msg = MissedMessage.sms(req.getRequestId(), 
												cursor.getString(cursor.getColumnIndex("phonebook_name")),
												cursor.getString(cursor.getColumnIndex("address")),
												cursor.getLong(cursor.getColumnIndex("date")) / 1000,
												cursor.getString(cursor.getColumnIndex("body")));
			req.addMesssage(msg);
		}
	}
	
	private void addMissedMms(Request req) {
		Uri allMessages = Uri.parse("content://mms/inbox");
		final String[] projection = {"_id", "phonebook_name", "address", "date", "title", "_data"};
		final String queryOption = "read = ? and date between ? and ? ";
		final String[] optionValue = {"0", String.valueOf(startDate * 1000), String.valueOf(endDate * 1000)};
		
		Cursor cursor = cr.query(allMessages, projection,
				queryOption, optionValue, null);
		
		while (cursor.moveToNext()) {
			MissedMessage msg = MissedMessage.mms(req.getRequestId(), 
					cursor.getString(cursor.getColumnIndex("phonebook_name")),
					cursor.getString(cursor.getColumnIndex("address")),
					cursor.getLong(cursor.getColumnIndex("date")) / 1000,
					cursor.getString(cursor.getColumnIndex("title")),
					readDetailMmsContent(cursor.getString(cursor.getColumnIndex("_data"))));
			
			req.addMesssage(msg);
		}

	}
	
	// TODO not implemented
	private byte[] readDetailMmsContent(String filePath) {
		return null;
	}


}
